/**********************************************************
 
***********************************************************
File Name		     : FileLogger.cs
Project Code		 : 
Author  			 : 
Created on	    	 : 
***********************************************************
Type		    	 : C# Source file
Purpose			     : This class has static method to log the 
                       Error/Exception to textfiles date wise.
Pre-requisites		 : 
Known Bugs		     : 
Associated File(s)	 :
Table(s)/Procedure(s):
Method(s)    		 :WriteToFile
See Also			 :
***********************************************************
Revision History	 :


***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Configuration;

namespace ExceptionLogger
{
    class FileLogger
    {
        /// <summary>
        /// Method to Log the Error/exception to a file on daily basis.
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="ClientID"></param>
        /// <param name="UserName"></param>
        public static void WriteToFile(Exception ex, string ClientID, string UserName)
        {
            if (ex != null)
            {
                StringBuilder sbErrorDetails;
                StreamWriter strWriter;

                string MonthfolderName = string.Empty;
                string fileName = string.Empty;
                string ExceptionFolderName = "Exception_Logs";
                //string folderPath = string.Empty;
                string filePath = string.Empty;

                sbErrorDetails = new StringBuilder();

                //To get the folder/directory Name
                MonthfolderName = string.Format("{0:MMMM}", DateTime.Now) + "_" + DateTime.Now.Year.ToString();

                //To get the file/textfile Name

                fileName = DateTime.Now.Day.ToString() + "_" + string.Format("{0:MMM}", DateTime.Now) + "_" + DateTime.Now.Year.ToString();

                //To set the Folder Path
                //folderPath = "~/" + ExceptionFolderName + "/" + MonthfolderName;


                string FullFolderPath = AppDomain.CurrentDomain.BaseDirectory;
                FullFolderPath += ExceptionFolderName + "\\" + MonthfolderName;


                //Checks if the Directory already Exists
                if (!Directory.Exists(FullFolderPath))
                {
                    Directory.CreateDirectory(FullFolderPath);
                }

                //To set the File path
                filePath = FullFolderPath + "\\" + fileName + ".txt";
                //System.Web.HttpContext.Current.Server.MapPath(folderPath) 

                sbErrorDetails.AppendLine("********************************************************************************************************************");
                sbErrorDetails.AppendLine("Time             -- " + DateTime.Now);
                sbErrorDetails.AppendLine("Message          -- " + ex.Message);
                sbErrorDetails.AppendLine("StackTrace       -- " + ex.StackTrace);
                sbErrorDetails.AppendLine("ExceptionType    -- " + ex.GetType().ToString());
                sbErrorDetails.AppendLine("TargetSite       -- " + ex.TargetSite.ToString());
                sbErrorDetails.AppendLine("UserName         -- " + UserName);
                sbErrorDetails.AppendLine("ClientID         -- " + ClientID);
                sbErrorDetails.AppendLine("********************************************************************************************************************");

                //To Create/append a file and log the error.
                strWriter = File.AppendText(filePath);



                strWriter.WriteLine(sbErrorDetails);
                strWriter.Close();
            }

        }


        /// <summary>
        /// Method to Log the Error/exception to a file on daily basis.
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="ClientID"></param>
        /// <param name="UserName"></param>
        public static void WriteToFile(string ex)
        {
            StringBuilder sbErrorDetails;
            StreamWriter strWriter;

            string MonthfolderName = string.Empty;
            string fileName = string.Empty;
            string LogFolderName = "Import_Logs";
            string filePath = string.Empty;

            sbErrorDetails = new StringBuilder();

            //To get the folder/directory Name
            MonthfolderName = string.Format("{0:MMMM}", DateTime.Now) + "_" + DateTime.Now.Year.ToString();

            //To get the file/textfile Name

            fileName = DateTime.Now.Day.ToString() + "_" + string.Format("{0:MMM}", DateTime.Now) + "_" + DateTime.Now.Year.ToString();

            string FullFolderPath = string.Empty;
            //AppDomain.CurrentDomain.BaseDirectory;
            FullFolderPath += LogFolderName + "\\" + MonthfolderName;


            //Checks if the Directory already Exists
            if (!Directory.Exists(FullFolderPath))
            {
                Directory.CreateDirectory(FullFolderPath);
            }

            //To set the File path
            filePath = FullFolderPath + "\\" + fileName + ".txt";

            sbErrorDetails.AppendLine("Time             -- " + DateTime.Now);
            sbErrorDetails.AppendLine("Message          -- " + ex);
            sbErrorDetails.AppendLine("********************************************************************************************************************");

            //To Create/append a file and log the error.
            strWriter = File.AppendText(filePath);



            strWriter.WriteLine(sbErrorDetails);
            strWriter.Close();

        }
    }
}

